#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;

const char* file_adress = "/home/afeng/linux/Test.23.9.30/mypro";
                  // 绝对路径："./mypro"
     char*const env[10] = {(char*)"env_23=290764", NULL};

int main()
{
   pid_t pd = fork();
   if (pd == 0)
   {
    // 子进程
     cout << "子进程开始, pid:" << getpid() <<  endl;
     char* _argv[10] = {(char*)"ls", (char*)"-l", (char*)"-a",(char*)"--color=auto",  NULL};
     //execv("/usr/bin/ls", _argv);
     // execlp("ls", "ls", "-ll", "-a","--color=auto",  NULL);
     execle(file_adress, "mypro", "-a", "-l", NULL, env);
     exit(-1);

   }else if (pd)
   {
     // 父进程
     int status = 100;
     cout << "父进程开始" << endl;
     pid_t ret = waitpid(-1, &status, 0);
     if (ret)
     {
       cout << "子进程退出，打印子进程退出码:" << WEXITSTATUS(status) <<endl;
     }else 
     {
       cout << "子进程未退出" << endl;
     }
   }
   else 
   {
     cout << "创建子进程失败" <<endl;
   }
  return 0;
}
